package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CarInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.Station;
import com.suyuan.entity.TransportRouteInfor;
import com.suyuan.service.CarInfoService;
import com.suyuan.service.StationService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.StationVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 *  车辆管理控制类
 *
 */
@Controller
@RequestMapping(value = "/admin/station")
public class StationController {

    @Resource(name = "stationService")
    private StationService stationService;

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/delete" , method = RequestMethod.POST)
    @ResponseBody
    public Object delete(Integer id){

        int code = stationService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add" , method = RequestMethod.GET)
    public String add(ModelMap model){
        //获取所有公司列表
        List<TransportRouteInfor> routes = stationService.getRoutes();
        model.addAttribute("routes",routes);
        return "station_add";
    }


    /**
     * 保存
     * @return
     */
    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    @ResponseBody
    public Object save(Station station){
        int code = 0;
        try {
            code = stationService.add(station);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/list")
    public Object list(ModelMap model){
        List<StationVO> stations = stationService.list();
        model.addAttribute("stations",stations);
        return "station_list";
    }

    /**
     * 批量删除
     * @return
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByids(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
           idArr = ids.split(",");
        }else{
            idArr = new String[]{ ids };
        }
        for(String id : idArr){
            stationService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        Station station = stationService.edit(id);
        List<TransportRouteInfor> routes = stationService.getRoutes();
        model.addAttribute("station",station);
        model.addAttribute("routes",routes);
        return "station_edit";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(Station station){
        int code = stationService.update(station);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }


    /*局部时间类型转换器*/
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }*/

}
